package org.opsli.modulars.business.patrolTask.web;

import org.opsli.api.wrapper.business.patrolRouteRecord.PatrolRouteRecordModel;
import org.opsli.api.wrapper.business.patrolTask.PatrolTaskModel;
import org.opsli.api.wrapper.system.user.UserModel;
import org.opsli.core.utils.UserUtil;
import org.opsli.modulars.business.app.web.ZkUtils;
import org.opsli.modulars.business.patrolRouteRecord.service.IPatrolRouteRecordService;
import org.opsli.modulars.business.patrolTask.service.IPatrolTaskService;
import org.opsli.modulars.business.userRoute.entity.UserRoute;
import org.opsli.modulars.business.userRoute.service.IUserRouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;
import java.util.List;

import static org.apache.shiro.web.filter.mgt.DefaultFilter.user;

/**
 * 郑州创源公司 定时下发任务
 */
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任
public class TimingTask {

    @Autowired
    private IUserRouteService userRouteService;
    @Autowired
    private IPatrolTaskService taskService;
    @Autowired
    private IPatrolRouteRecordService routeRecordService;
    @Autowired
    private IPatrolTaskService iPatrolTaskService;



    //3.添加定时任务 凌晨一点
    @Scheduled(cron = "0 0 1 * * ?")
    private void configureTasks() {

        System.err.println("执行静态定时任务时间: " + LocalDateTime.now());

        String devid="41010";
        List<UserModel> userList = UserUtil.getDevidList(devid);

        for (UserModel user :userList){
            //无任务,需要创建
            //获取当前用户路线
            List<UserRoute> routes = userRouteService.getMyList(user.getId());
            if (null != routes || routes.size() == 0) {
                for (UserRoute route : routes) {
                    String sign = ZkUtils.getRandomStr();
                    //创建任务
                    PatrolTaskModel taskModel = new PatrolTaskModel();
                    taskModel.setDevid(user.getCompanyId());
                    taskModel.setDevname(user.getCompany());
                    taskModel.setUserId(user.getId());
                    taskModel.setUserName(user.getRealName());
                    taskModel.setRouteId(route.getRouteId());
                    taskModel.setRouteName(route.getRouteName());
                    taskModel.setSign(sign);
                    taskModel.setType("0");
                    taskModel.setState("0");
                    taskModel.setManager(user.getId());
                    taskService.insert(taskModel);
                    //创建巡线线路
                    PatrolRouteRecordModel recordModel=new PatrolRouteRecordModel();
                    recordModel.setDevid(user.getCompanyId());
                    recordModel.setDevname(user.getCompany());
                    recordModel.setUserId(user.getId());
                    recordModel.setUserName(user.getRealName());
                    recordModel.setRouteId(route.getRouteId());
                    recordModel.setRouteName(route.getRouteName());
                    recordModel.setSign(sign);
                    recordModel.setType("0");
                    recordModel.setState("0");
                    routeRecordService.insert(recordModel);
                }
            }
        }

    }
}
